package com.ruoyi.schedule.service.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import com.ruoyi.common.core.page.R;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.document.domain.Contract;
import com.ruoyi.personnel.domain.Employee;
import com.ruoyi.personnel.mapper.EmployeeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.schedule.mapper.ProgrammeMapper;
import com.ruoyi.schedule.domain.Programme;
import com.ruoyi.schedule.service.IProgrammeService;

/**
 * 日程信息管理Service业务层处理
 *
 * @author ruoyi
 * @date 2023-03-07
 */
@Service
public class ProgrammeServiceImpl implements IProgrammeService {
    @Autowired
    private ProgrammeMapper programmeMapper;
    @Autowired
    private EmployeeMapper employeeMapper;


    /**
     * 查询日程信息管理
     *
     * @param proId 日程信息管理主键
     * @return 日程信息管理
     */
    @Override
    public Programme selectProgrammeByProId(Integer proId) {
        return programmeMapper.selectProgrammeByProId(proId);
    }

    /**
     * 查询日程信息管理列表
     *
     * @param programme 日程信息管理
     * @return 日程信息管理
     */
    @Override
    public List<Programme> selectProgrammeList(Programme programme) {
        List<Programme> programmes = programmeMapper.selectProgrammeList(programme);
        for (Programme programme1 : programmes) {
            Employee employee = employeeMapper.selectEmployeeByEmpId(programme1.getEmpId());
            //Programme programme2 = programmeMapper.selectProgrammeByProId(programme1.getProId());
            // programme1.setEmpName(programme2.getEmpName());
            programme1.setEmpName(employee.getEmpName());
            //获取截至时间
            Date deadline = programme1.getDeadline();
            if (deadline != null) {
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
                Date date = new Date();
                long a = deadline.getTime() - date.getTime();
                if (a < 0) {
                    long b = 0 - (a / 3600 / 24 / 1000);
                    System.out.println("123123：" + b);
                    programme1.setIsBlack("已逾期" + b + "天");
                } else {
                    //String format = simpleDateFormat.format(deadline);
                    //programme1.setIsBlack(format);
                }
            } else {
                programme1.setIsBlack(null);
            }

        }

        programmeMapper.updateStatus();
        return programmes;
    }

    /**
     * 新增日程信息管理
     *
     * @param programme 日程信息管理
     * @return 结果
     */
    @Override
    public boolean insertProgramme(Programme programme) {
        // 开始时间
        Date contractTime = programme.getProTime();
        long oldTime = contractTime.getTime();
        // 截至时间
        Date effectiveTime = programme.getDeadline();
        long newTime = 0L;
        long newTime1 = System.currentTimeMillis();
        if (oldTime >= newTime1) {
            oldTime = contractTime.getTime();
            return false;
        }
        if (effectiveTime != null) {
            newTime = effectiveTime.getTime();
        } else {
            newTime = System.currentTimeMillis();
        }
        if (oldTime < newTime) {
            //新增
            programme.setCreateTime(DateUtils.getNowDate());
            programmeMapper.insertProgramme(programme);
            return true;
        }
        return false;
//        programme.setCreateTime(DateUtils.getNowDate());
//        return programmeMapper.insertProgramme(programme);
    }

    /**
     * 修改日程信息管理
     *
     * @param programme 日程信息管理
     * @return 结果
     */
    @Override
    public boolean updateProgramme(Programme programme) {
        //截至时间
//        Date effectiveTime1 = programme.getEffectiveTime();
//        System.out.println("预计生效时间"+effectiveTime1);
        // 开始时间
        Date contractTime = programme.getProTime();
        long oldTime = contractTime.getTime();
        // 截至时间
        Date effectiveTime = programme.getDeadline();
        long newTime = 0L;
        long newTime1 = System.currentTimeMillis();
        if (oldTime >= newTime1) {
            oldTime = contractTime.getTime();
            return false;
        }
        if (effectiveTime != null) {
            newTime = effectiveTime.getTime();
        } else {
            newTime = System.currentTimeMillis();
        }
        if (oldTime < newTime) {
            //新增
            programme.setUpdateTime(DateUtils.getNowDate());
            programmeMapper.updateProgramme(programme);
            return true;
        }
        return false;
//        programme.setUpdateTime(DateUtils.getNowDate());
//        return programmeMapper.updateProgramme(programme);
    }

    /**
     * 批量删除日程信息管理
     *
     * @param proIds 需要删除的日程信息管理主键
     * @return 结果
     */
    @Override
    public boolean deleteProgrammeByProIds(Integer[] proIds) {
        for (int i = 0; i < proIds.length; i++) {
            System.out.println("批量删除日程管理的"+proIds[i]);
            // 根据id查询programme
            Programme programme = programmeMapper.selectProgrammeByProId(proIds[i]);
            //获取截至时间
            Date deadline = programme.getDeadline();
            if (deadline== null) return false;
                // 获取数据库字段的Deadline的时间毫秒值
                long abEndTime_ms = deadline.getTime();
                //当前时间毫秒值
                long nowTime_ms = System.currentTimeMillis();
                // Programme programme = programmeMapper.selectProgrammeByProId(proIds[i]);
                //获取状态
                String status = programme.getStatus();
                if ("0".equals(status)) {
                    if (deadline != null) {
                    if (abEndTime_ms <= nowTime_ms) {
//                vacateMapper.deleteVacateByAbId(abIds[i]);
                        programmeMapper.deleteProgrammeByProIds(proIds);
                    } else {
                        return false;
                    }
                     }
                }

        }
        return true;

/*        String[] arr = new String[proIds.length];
        for (int i = 0; i < proIds.length; i++) {
            System.out.println("删除的id值--" + proIds[i]);
            //根据id查询出时间
            // Contract vacate = contractMapper.selectContractByConId(conIds[i]);
            Programme programme = programmeMapper.selectProgrammeByProId(proIds[i]);
            String status = programme.getStatus();
            if ("1".equals(status) || "2".equals(status)) {
                //数据库结束时间
                Date deadline = programme.getDeadline();
                if (deadline != null) {

                    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
                    String abEndTime01 = simpleDateFormat.format(deadline);
                    System.out.println("数据库结束时间" + abEndTime01);
                    //当前时间
                    Date date = new Date();
                    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
                    String nowTime = formatter.format(date);
                    System.out.println();
                    //与当前时间做比较
                    Date abEndTime02 = null;
                    Date nowTime02 = null;
                    try {
                        abEndTime02 = simpleDateFormat.parse(abEndTime01);
                        nowTime02 = simpleDateFormat.parse(nowTime);
                    } catch (ParseException e) {
                        e.printStackTrace();
                    }
                    //是否超过
                    boolean before = abEndTime02.before(nowTime02);
                    if (before == true) {
                        //删除
                        arr[i] = "0";
                    } else {
                        arr[i] = "1";
                    }
                } else {
//                for (int i1 = 0; i1 < arr.length; i1++) {
//                    arr[i1] = "1";
//                }
                    arr[0] = "1";
                }
            }
        }
        //数据
        String str = Arrays.toString(arr);
        if (str.contains("1")) {
            return false;
        } else {
            programmeMapper.deleteProgrammeByProIds(proIds);
            return true;
        }
        return programmeMapper.deleteProgrammeByProIds(proIds);*/
    }

    /**
     * 删除日程信息管理信息
     *
     * @param proId 日程信息管理主键
     * @return 结果
     */
    @Override
    public int deleteProgrammeByProId(Integer proId) {
        return programmeMapper.deleteProgrammeByProId(proId);
    }

    /**
     * 回滚
     *
     * @param array
     */
    @Override
    public void rollbackEmployee(Object[] array) {
        programmeMapper.rollbackEmployee(array);
    }
}
